package cn.codingtalk.tacomallinterfaceportal.feign;

import feign.hystrix.FallbackFactory;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import cn.codingtalk.tacomallcommon.dto.SendDto;
import cn.codingtalk.tacomallcommon.dto.ReturnDto;
import cn.codingtalk.tacomallinterfaceportal.entity.member.Member;

@FeignClient(name = "middle-platform-biz-member", fallbackFactory = MemberFeignClientFallbackFactory.class)
public interface MemberFeignClient {
    @RequestMapping(value = "/member/findById", method = RequestMethod.POST)
    ReturnDto findById(@RequestBody SendDto sendDto);

    @RequestMapping(value = "/member/add", method = RequestMethod.POST)
    ReturnDto add(@RequestBody SendDto sendDto);
}

@Component
class MemberFeignClientFallbackFactory implements FallbackFactory<MemberFeignClient> {
    @Override
    public MemberFeignClient create(Throwable throwable) {
        return new MemberFeignClient() {
            @Override
            public ReturnDto findById(SendDto sendDto) {
                ReturnDto returnDto = new ReturnDto();
                returnDto.setData(new Member(1, "defaut", 12, "default"));
                return returnDto;
            }

            @Override
            public ReturnDto add(SendDto sendDto) {
                ReturnDto returnDto = new ReturnDto();
                returnDto.setStatus(400);
                return returnDto;
            }
        };
    }
}

